<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 顶部说明 -->
				<view class="instruction">
					<text class="instruction-title">数字起卦</text>
					<text class="instruction-text">请输入两个1-8之间的数字，数字蕴含着宇宙的规律</text>
				</view>
				
				<!-- 问题输入 -->
				<app-card>
					<view class="question-section">
						<text class="section-title">您的问题</text>
						<textarea 
							v-model="question" 
							placeholder="请输入您想要占卜的问题..."
							class="question-input"
							maxlength="200"
						></textarea>
					</view>
				</app-card>
				
				<!-- 数字输入 -->
				<app-card>
					<view class="number-section">
						<text class="section-title">选择数字</text>
						<view class="number-description">
							<text class="desc-text">请选择两个数字（1-8），分别对应上卦和下卦</text>
						</view>
						
						<view class="number-inputs">
							<view class="number-input-group">
								<text class="input-label">上卦数字</text>
								<view class="number-grid">
									<view 
										v-for="num in 8" 
										:key="num" 
										class="number-item" 
										:class="{ 'active': upperNumber === num }"
										@tap="selectUpperNumber(num)"
									>
										<text class="number-text">{{ num }}</text>
									</view>
								</view>
							</view>
							
							<view class="number-input-group">
								<text class="input-label">下卦数字</text>
								<view class="number-grid">
									<view 
										v-for="num in 8" 
										:key="num" 
										class="number-item" 
										:class="{ 'active': lowerNumber === num }"
										@tap="selectLowerNumber(num)"
									>
										<text class="number-text">{{ num }}</text>
									</view>
								</view>
							</view>
						</view>
						
						<!-- 随机选择按钮 -->
						<view class="random-section">
							<app-button 
								:text="'随机选择'" 
								type="secondary"
								@tap="randomSelect"
							></app-button>
						</view>
					</view>
				</app-card>
				
				<!-- 已选择的数字显示 -->
				<app-card v-if="upperNumber && lowerNumber">
					<view class="selected-section">
						<text class="section-title">已选择的数字</text>
						<view class="selected-numbers">
							<view class="selected-number">
								<text class="number-label">上卦</text>
								<text class="number-value">{{ upperNumber }}</text>
							</view>
							<text class="separator">+</text>
							<view class="selected-number">
								<text class="number-label">下卦</text>
								<text class="number-value">{{ lowerNumber }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 起卦按钮 -->
				<view class="action-section">
					<app-button 
						:text="'开始起卦'" 
						type="primary"
						@tap="generateHexagram"
						:disabled="!question || !upperNumber || !lowerNumber"
					></app-button>
				</view>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import apiService from '@/utils/api-service.js'

export default {
	name: 'NumberDivinationPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			question: '',
			upperNumber: null,
			lowerNumber: null
		}
	},
	onLoad() {
		// 页面加载时的初始化逻辑
	},
	methods: {
		selectUpperNumber(num) {
			this.upperNumber = num;
		},
		
		selectLowerNumber(num) {
			this.lowerNumber = num;
		},
		
		randomSelect() {
			this.upperNumber = Math.floor(Math.random() * 8) + 1;
			this.lowerNumber = Math.floor(Math.random() * 8) + 1;
			
			uni.showToast({
				title: `已选择: ${this.upperNumber} 和 ${this.lowerNumber}`,
				icon: 'none'
			});
		},
		
		async generateHexagram() {
			if (!this.question) {
				uni.showToast({
					title: '请输入问题',
					icon: 'none'
				});
				return;
			}
			
			if (!this.upperNumber || !this.lowerNumber) {
				uni.showToast({
					title: '请选择两个数字',
					icon: 'none'
				});
				return;
			}
			
			// 检查网络状态
			const isNetworkAvailable = await apiService.checkNetworkStatus();
			if (!isNetworkAvailable) {
				uni.showToast({
					title: '网络连接不可用',
					icon: 'none'
				});
				return;
			}
			
			uni.showLoading({
				title: '正在起卦...'
			});
			
			try {
				const response = await apiService.numberDivination({
					question: this.question,
					upperNumber: this.upperNumber,
					lowerNumber: this.lowerNumber
				});
				
				uni.hideLoading();
				
				if (response.success) {
					// 跳转到结果页面
					uni.navigateTo({
						url: `/pages/divination-result/divination-result?type=number&question=${encodeURIComponent(this.question)}&result=${encodeURIComponent(JSON.stringify(response.data))}&upperNumber=${this.upperNumber}&lowerNumber=${this.lowerNumber}`
					});
				} else {
					uni.showToast({
						title: response.message || '起卦失败',
						icon: 'none'
					});
				}
			} catch (error) {
				console.error('起卦失败:', error);
				uni.hideLoading();
				apiService.handleError(error);
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.instruction {
	text-align: center;
	margin-bottom: 48rpx;
	
	.instruction-title {
		display: block;
		font-size: 48rpx;
		font-weight: bold;
		color: #000000;
		margin-bottom: 16rpx;
	}
	
	.instruction-text {
		display: block;
		font-size: 28rpx;
		color: rgba(0, 0, 0, 0.7);
		line-height: 1.5;
	}
}

.question-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 24rpx;
	}
	
	.question-input {
		width: 100%;
		min-height: 120rpx;
		padding: 24rpx;
		border: 2rpx solid #f0f0f0;
		border-radius: 16rpx;
		font-size: 28rpx;
		line-height: 1.5;
		background-color: #fafafa;
	}
}

.number-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 24rpx;
	}
	
	.number-description {
		margin-bottom: 32rpx;
		
		.desc-text {
			font-size: 28rpx;
			color: rgba(0, 0, 0, 0.7);
			line-height: 1.4;
		}
	}
	
	.number-inputs {
		.number-input-group {
			margin-bottom: 40rpx;
			
			.input-label {
				display: block;
				font-size: 28rpx;
				color: #000000;
				margin-bottom: 16rpx;
			}
			
			.number-grid {
				display: grid;
				grid-template-columns: repeat(4, 1fr);
				gap: 16rpx;
				
				.number-item {
					aspect-ratio: 1;
					background-color: #fafafa;
					border: 2rpx solid #f0f0f0;
					border-radius: 16rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					transition: all 0.2s ease;
					
					&.active {
						background: linear-gradient(135deg, #FFFAE0 0%, #FFF7C2 100%);
						border-color: #6A82FB;
					}
					
					.number-text {
						font-size: 40rpx;
						font-weight: 500;
						color: #000000;
					}
				}
			}
		}
	}
	
	.random-section {
		text-align: center;
		margin-top: 32rpx;
	}
}

.selected-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 24rpx;
	}
	
	.selected-numbers {
		display: flex;
		align-items: center;
		justify-content: center;
		gap: 32rpx;
		
		.selected-number {
			text-align: center;
			
			.number-label {
				display: block;
				font-size: 24rpx;
				color: rgba(0, 0, 0, 0.7);
				margin-bottom: 8rpx;
			}
			
			.number-value {
				display: block;
				font-size: 64rpx;
				font-weight: bold;
				color: #6A82FB;
			}
		}
		
		.separator {
			font-size: 48rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.action-section {
	margin-top: 48rpx;
	text-align: center;
}
</style>